package alan.springboot2x.swagger.knife4j.controller;

import alan.springboot2x.swagger.knife4j.bean.Result;
import alan.springboot2x.swagger.knife4j.config.CustomerBlockHandler;
import alan.springboot2x.swagger.knife4j.service.MyTestService;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import io.micrometer.core.annotation.Timed;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "测试模块")
@RestController
@RequestMapping("myTest")
@Slf4j
public class MyTestController {

    @Autowired
    private MyTestService myTestService;

    /**
     * see : localhost:19081/myTest/test
     * @param request
     * @return
     */
    @ApiOperation(value="测试接口", notes="测试接口")
    @PostMapping("test")
    @Timed(value = "all.test", longTask = true)
    public Result test(HttpServletRequest request){
        Map<String, String> headerMap = ServletUtil.getHeaderMap(request);
        Map<String, String> paramMap = ServletUtil.getParamMap(request);
        String body = ServletUtil.getBody(request);
        log.info("headerMap:{}", JSONUtil.toJsonPrettyStr(headerMap));
        log.info("paramMap:{}", JSONUtil.toJsonPrettyStr(paramMap));
        log.info("body:{}", body);
        return myTestService.getStr();
    }

    @ApiOperation(value="测试接口2", notes="测试接口2")
    @PostMapping("test2")
    public Result test2(HttpServletRequest request){
        Map<String, String> headerMap = ServletUtil.getHeaderMap(request);
        Map<String, String> paramMap = ServletUtil.getParamMap(request);
        String body = ServletUtil.getBody(request);
        log.info("headerMap:{}", JSONUtil.toJsonPrettyStr(headerMap));
        log.info("paramMap:{}", JSONUtil.toJsonPrettyStr(paramMap));
        log.info("body:{}", body);
        return myTestService.getStr2();
    }

    @PostMapping("test3")
    public Result test3(HttpServletRequest request){
        Map<String, String> headerMap = ServletUtil.getHeaderMap(request);
        Map<String, String> paramMap = ServletUtil.getParamMap(request);
        String body = ServletUtil.getBody(request);
        log.info("headerMap:{}", JSONUtil.toJsonPrettyStr(headerMap));
        log.info("paramMap:{}", JSONUtil.toJsonPrettyStr(paramMap));
        log.info("body:{}", body);
        return myTestService.getStr3();
    }

    public static void main(String[] args) {
        String tt = "\u200bapplication/x-www-form-urlencoded";
        System.out.println(tt);
        System.out.println(tt.length());
    }


}
